package com.tybbank.Fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.tybbank.Bean.TeamBean;
import com.tybbank.MyApplication;
import com.tybbank.R;
import com.tybbank.Utils.Constant;
import com.tybbank.Utils.MD5Utils;
import com.tybbank.Utils.logUtilssss;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import okhttp3.Call;

/**
 * Created by Administrator on 2018/5/31 0031.
 */

public class GoodsFragment extends Fragment {


    @BindView(R.id.wu)
    TextView wu;
    @BindView(R.id.tv_zhitui_num)
    TextView tvZhituiNum;
    @BindView(R.id.wu1)
    TextView wu1;
    @BindView(R.id.tv_tuandui_num)
    TextView tvTuanduiNum;
    @BindView(R.id.rl_RecyclerView_list)
    RecyclerView rv_recyclerView;
    @BindView(R.id.ptr_scroll_view)
    PullToRefreshScrollView ptrScrollView;
    @BindView(R.id.rl_gone)
    RelativeLayout rlGone;
    Unbinder unbinder;
    @BindView(R.id.tv_zhanghao)
    TextView tvZhanghao;
    @BindView(R.id.tv_shequyeji)
    TextView tvShequyeji;
    @BindView(R.id.wu11)
    TextView wu11;
    private int page = 1;
    private List<TeamBean.ListsBean> list = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_goods, container, false);
        unbinder = ButterKnife.bind(this, view);
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        rv_recyclerView.setLayoutManager(manager);
        rv_recyclerView.setHasFixedSize(true);//每个 高度一样
        page = 1;
        rv_recyclerView.setNestedScrollingEnabled(false);
        ptrScrollView.setMode(PullToRefreshBase.Mode.BOTH);
        ptrScrollView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                page = 1;
                initdata();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                page++;
                initdata();
            }
        });
        initdata();
        return view;
    }
    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            page = 1;
            initdata();
        }
    }
    private void initdata() {
        String time = MD5Utils.getTime();
        SortedMap<String, String> parameters = new TreeMap<String, String>(
                new Comparator<String>() {
                    public int compare(String obj1, String obj2) {
                        // 降序排序
                        return obj1.compareTo(obj2);
                    }
                });
        parameters.put("uuid", MyApplication.uuid);
        parameters.put("token", MyApplication.token);
        parameters.put("TimeStamp", time);
        parameters.put("page", page + "");
        String sign = MD5Utils.createSign(parameters);
        OkHttpUtils
                .post()
                .url(Constant.team_list)
                .addParams("uuid", MyApplication.uuid)
                .addParams("token", MyApplication.token)
                .addParams("TimeStamp", time)
                .addParams("Sign", sign)
                .addParams("page", page + "")
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                        ptrScrollView.onRefreshComplete();
                        Toast.makeText(getActivity(), getString(R.string.error), Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        logUtilssss.e("TAG我的团队" + response.toString());
                        ptrScrollView.onRefreshComplete();
                        postReponse(response);
                    }

                    public void postReponse(String json) {
                        try {
                            TeamBean enterBean = new Gson().fromJson(json, new TypeToken<TeamBean>() {
                            }.getType());
                            int a = enterBean.getCode();
                            if (a == 1011) {
                                TeamBean<List<TeamBean.ListsBean>> enter = new Gson().fromJson(json, new TypeToken<TeamBean<List<TeamBean.ListsBean>>>() {
                                }.getType());
                                tvZhituiNum.setText(enterBean.getTeam_count());
                                tvTuanduiNum.setText(enterBean.getTeam_number());
                                tvShequyeji.setText(enterBean.getTeam_money());
                                tvZhanghao.setText(getString(R.string.zhanghao)+": "+enterBean.getAccount());
                                if (page == 1) {
                                    list.clear();
                                    //商品
                                    list = enter.getLists();
                                }
                                if (page > 1) {
                                    list.addAll(enter.getLists());
                                }
                                if (page == 1 && list.isEmpty()) {
                                    rlGone.setVisibility(View.VISIBLE);
                                    rv_recyclerView.setVisibility(View.GONE);
                                    return;
                                }
                                //商品
                                buylistdapter adapte = new buylistdapter();
                                rv_recyclerView.setAdapter(adapte);
                                adapte.notifyDataSetChanged();
                            } else {

                            }
                        } catch (Exception e) {

                        }

                    }
                });

    }

    public class buylistdapter extends RecyclerView.Adapter {

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(getActivity()).inflate(R.layout.team_adapter_item, parent, false);
            return new MYholder(view);
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
            MYholder holder1 = (MYholder) holder;
            holder1.setdata(position);
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        private class MYholder extends RecyclerView.ViewHolder {

            private final TextView tv_look, tv_time, tv_acction, tv_phone;

            public MYholder(View view) {
                super(view);
                tv_time = view.findViewById(R.id.tv_time);
                tv_phone = view.findViewById(R.id.tv_phone);
                tv_acction = view.findViewById(R.id.tv_acction);
                tv_look = view.findViewById(R.id.tv_look);
            }
            public void setdata(final int position) {   // is_proving 0 未实名认证 1 已实名认证 activation 0 未激活 1 已激活
                tv_acction.setText(getString(R.string.zhanghao)+": " + list.get(position).getAccount());
                tv_time.setText(list.get(position).getTeam_money());
//                if (list.get(position).getIs_proving().equals("0")){
//                    tv_time.setText(getString(R.string.shiming_no));
//                }else {
//                    tv_time.setText(getString(R.string.shiming_yes));
//                }
                tv_phone.setText(getString(R.string.zhucetime)+"：" + (list.get(position).getCreate_time()));
                if (list.get(position).getActivation().equals("0")){
                    tv_look.setText(getString(R.string.jihuo_no));
                }else {
                    tv_look.setText(getString(R.string.jihuo_yes));
                }
            }
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
